package com.jvtd.eyl.ui.main.home.classInfo.classPeriodical;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jvtd.eyl.R;
import com.jvtd.eyl.base.BaseMvpFragment;
import com.jvtd.eyl.bean.binding.PeriodicalBean;
import com.jvtd.eyl.databinding.FragmentClassPeriodicalBinding;
import com.jvtd.utils.SmartRefreshUtils;
import com.jvtd.widget.emptyView.JvtdEmptyView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import javax.inject.Inject;

/**
 * 期刊二级分类信息
 *
 * 作者:chenlei
 * 时间:2018/11/13 4:54 PM
 */
public class ClassPeriodicalFragment extends BaseMvpFragment implements ClassPeriodicalMvpView, OnRefreshListener {
    @Inject
    ClassPeriodicalPresenter<ClassPeriodicalMvpView> mPresenter;
    private FragmentClassPeriodicalBinding mBinding;
    private static final String ID_KEY = "ID_KEY";
    private static final String SECONDARY_ID_KEY = "SECONDARY_ID_KEY";
    private String id;
    private String secondaryId;

    public static ClassPeriodicalFragment newInstance(String id, String secondaryId)
    {
        Bundle args = new Bundle();
        args.putString(ID_KEY,id);
        args.putString(SECONDARY_ID_KEY,secondaryId);
        ClassPeriodicalFragment fragment = new ClassPeriodicalFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected View getLayout(LayoutInflater inflater, @Nullable ViewGroup container) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_class_periodical,container,false);
        return mBinding.getRoot();
    }

    @Override
    protected void initViewAndData() {
        mPresenter.onAttach(this);
        if (getArguments() != null)
        {
            id = getArguments().getString(ID_KEY);
            secondaryId = getArguments().getString(SECONDARY_ID_KEY);
        }

        if(TextUtils.isEmpty(id) || TextUtils.isEmpty(secondaryId))
        {
            onError(R.string.error_message);
            return;
        }

        initRefreshLayout();

        initRecyclerView();

        onRefresh(null);
    }

    private void initRefreshLayout()
    {
        SmartRefreshUtils.initRefresh(mContext, mBinding.refreshLayout, this);
    }

    private void initRecyclerView()
    {
        mBinding.recyclerView.setRefreshLayout(mBinding.refreshLayout)
                .openEmptyView((view, state) -> {
                    if (state == JvtdEmptyView.STATE_ERROR)
                        onRefresh(null);
                });
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mPresenter.getClassPeriodicalList(id,secondaryId);
    }

    @Override
    public void getClassPeriodicalListSuccess(List<PeriodicalBean> periodicalBeans) {
        mBinding.recyclerView.setData(periodicalBeans);
    }

    @Override
    public void getClassPeriodicalListFailed() {
        mBinding.recyclerView.firstLoadFailed();
    }
}
